﻿// Decompiled with JetBrains decompiler
// Type: Microsoft.InfoCards.UIAgentMonitor
// Assembly: infocard, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
// MVID: 8E14765A-6610-409A-BA36-099A0642905D
// Assembly location: E:\git\ALLIDA\windll\infocard.exe

using Microsoft.InfoCards.Diagnostics;
using System;
using System.Collections.Generic;

namespace Microsoft.InfoCards
{
  internal class UIAgentMonitor
  {
    private static UIAgentMonitor s_singleton = (UIAgentMonitor) null;
    private static object s_syncRoot = new object();
    private Dictionary<string, UIAgentMonitorHandle> m_currentCallingUsers;
    private Dictionary<int, UIAgentMonitorHandle> m_currentTSSessions;

    private UIAgentMonitor()
    {
      this.m_currentCallingUsers = new Dictionary<string, UIAgentMonitorHandle>();
      this.m_currentTSSessions = new Dictionary<int, UIAgentMonitorHandle>();
    }

    public static UIAgentMonitor Instance()
    {
      if (UIAgentMonitor.s_singleton == null)
      {
        lock (UIAgentMonitor.s_syncRoot)
        {
          if (UIAgentMonitor.s_singleton == null)
            UIAgentMonitor.s_singleton = new UIAgentMonitor();
        }
      }
      return UIAgentMonitor.s_singleton;
    }

    public void AddNewClient(UIAgentMonitorHandle handle)
    {
      lock (UIAgentMonitor.s_syncRoot)
      {
        string userName = handle.UserName;
        int tsSessionId = handle.TsSessionId;
        if (this.m_currentCallingUsers.ContainsKey(userName))
          throw InfoCardTrace.ThrowHelperError((Exception) new ServiceBusyException(SR.GetString("ServiceInUseOnAnotherSession")));
        if (this.m_currentTSSessions.ContainsKey(tsSessionId))
          throw InfoCardTrace.ThrowHelperError((Exception) new ServiceBusyException(SR.GetString("ServiceInUseOnAnotherSession")));
        this.m_currentCallingUsers.Add(userName, handle);
        try
        {
          this.m_currentTSSessions.Add(tsSessionId, handle);
        }
        catch (Exception ex)
        {
          this.m_currentCallingUsers.Remove(userName);
          throw;
        }
      }
    }

    public void RemoveClient(UIAgentMonitorHandle handle)
    {
      lock (UIAgentMonitor.s_syncRoot)
      {
        string userName = handle.UserName;
        int tsSessionId = handle.TsSessionId;
        if (!string.IsNullOrEmpty(userName) && this.m_currentCallingUsers.ContainsKey(userName) && handle == this.m_currentCallingUsers[userName])
          this.m_currentCallingUsers.Remove(userName);
        if (!this.m_currentTSSessions.ContainsKey(tsSessionId) || handle != this.m_currentTSSessions[tsSessionId])
          return;
        this.m_currentTSSessions.Remove(tsSessionId);
      }
    }
  }
}
